package cn.ffcs.uec.insurance.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.insurance.action.bean.UecBmclassEditBean;
import cn.ffcs.uec.insurance.manager.UecBmManager;
import cn.ffcs.uec.insurance.model.UecBmclass;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecBmclassEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = -6096343177536382090L;

	private UecBmclassEditBean bean = new UecBmclassEditBean();

	private String opType;

	@Resource
	private UecBmManager uecBmManager;
	
	@Resource
	private IdentityCardConfigManager identityCardConfigManager;

	private UecBmclass uecBmclass;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
		
		
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecBmclassEditWin() throws Exception {
		this.bindCombobox();
		System.out.println(arg.get("opType").toString());
		opType = (String) arg.get("opType");
		if (opType.equals("add")) {
			bean.getUecBmclassEditWin().setTitle("险种新增");
			bean.getUecBmclassEditWin().setWidth("40%");
			
		} else if (opType.equals("edit")) {
			bean.getUecBmclassEditWin().setTitle("险种修改");
			PubUtil.fillBeanFromPo(arg.get("uecBmclass"), bean);
		}
	}
	
	/**
	 * 检测是否存在该员工
	 * @throws InterruptedException 
	 */
	public void onCheck() throws InterruptedException {
		uecBmclass = new UecBmclass();
		PubUtil.fillPoFromBean(bean, uecBmclass);
		//uecOutStaffManager.findUecOutStaffInfo(uecOutStaffInfo);
		/*if (uecOutStaffManager.findUecBmclass(uecBmclass) != null) {
			Messagebox.show("该员工已存在");
		} else {
			bean.getOne().setVisible(true);
			bean.getTwo().setVisible(true);
			bean.getThree().setVisible(true);
			bean.getFour().setVisible(true);
			bean.getFive().setVisible(true);
			bean.getSix().setVisible(true);
			//bean.getUecOutStaffEditWin().setWidth("60%");
			bean.getSaveBmclass().setVisible(true);
		}*/
	}
	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}
		uecBmclass = UecBmclass.newInstance();
		
		if (opType.equals("add")) {
			// 保存员工
			PubUtil.fillPoFromEditorBean(uecBmclass, bean);
			uecBmclass.setId(null);
			
			DateFormatter();
			//String info = uecOutStaffManager.addUecBmclass(uecBmclass);
			uecBmclass.addOnly();
			Events.postEvent("onOK", this.self, uecBmclass);
			onCancel();
			/*if (info != null && info.equals("haved")) {
				Messagebox.show("已存在该员工");
				return;
			} else if (info != null && info.equals("add")) {
				onCancel();
				Messagebox.show("新增成功！");
			} */
		} 
		if (opType.equals("edit")) {
			PubUtil.fillPoFromEditorBean(uecBmclass, bean);
			uecBmclass.setId((Long)arg.get("id"));
			DateFormatter();
			uecBmclass.updateOnly();
			Events.postEvent("onOK", this.self, uecBmclass);
			onCancel();
			Messagebox.show("更新成功！");
		}
		
		
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecBmclassEditWin().onClose();
	}
	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		
		List<NodeVo> nodeVoIsList = new ArrayList<NodeVo>();
		String[][] is = {{"Y", "是"}, {"N", "否"}};
		for (int i = 0; i < is.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(is[i][j]);
				nodeVo.setName(is[i][j + 1]);
			}
			nodeVoIsList.add(nodeVo);
		}
		//停用
		ListboxUtils.rendererForEdit(bean.getStopflag(), nodeVoIsList);
		//启用单位基金基数
		ListboxUtils.rendererForEdit(bean.getEnableorgbase(),nodeVoIsList);
		// 允许组织修改数据来源
		ListboxUtils.rendererForEdit(bean.getEditdatasource(),nodeVoIsList);
		//允许下级增加险种项目
		ListboxUtils.rendererForEdit(bean.getAllowadditem(),nodeVoIsList);
	}
	/**
	 * 校验
	 * @return
	 */
	private String checkUacStaffData() {
		// BaseInfo Check
		/*if (StrUtil.isNullOrEmpty(bean.getPsnName().getValue())) {
			return "请填写人员名";
		} else if (StrUtil.checkBlank(bean.getPsnName().getValue())) {
			return "人员名中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getIdNum().getValue())) {
			return "请填写证件号码";
		} else if (StrUtil.checkBlank(bean.getIdNum().getValue())) {
			return "证件号码中有空格";
		} */
		
		// IDCARD Real Name Check
		/*CertIdVarBaseInfo certIdVarBaseInfo = certIdVarBaseInfoManager
				.getCertIdVarInfo(bean.getPsnName()getValue(), bean.getIdNum()getValue());
		if (certIdVarBaseInfo) {
			String idNum = bean.getCertNumber().getValue();
			String idName = bean.getCertName().getValue();
			try {
				boolean isRealName = CertUtil.checkIdCard(idNum, idName);

				if (!isRealName) {
					return "实名认证未通过";
				}
			} catch (Exception e) {
				return "调用国政通接口失败";
			}

		}*/

		return null;
	}
	public void DateFormatter() {
		//SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		/*if (!StrUtil.isNullOrEmpty(bean.getBirthDate().getValue())) {
			// 出生日期
			uecBmclass.setBirthDate(formatter.format(bean.getBirthDate()
					.getValue()));
		}*/
	}
}
